package com.nexen.merchant.warrantyCard.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.nexen.common.utils.StringUtils;
import com.nexen.framework.manager.AsyncManager;
import com.nexen.framework.manager.factory.AsyncFactory;
import com.nexen.merchant.barcode.domain.NexenBarcode;
import com.nexen.merchant.barcode.mapper.NexenBarcodeMapper;
import com.nexen.system.domain.SysOperLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.nexen.merchant.warrantyCard.mapper.NexenWarrantycardMapper;
import com.nexen.merchant.warrantyCard.domain.NexenWarrantycard;
import com.nexen.merchant.warrantyCard.service.INexenWarrantycardService;

/**
 * 质保卡Service业务层处理
 * 
 * @author xiao
 * @date 2024-07-31
 */
@Service
public class NexenWarrantycardServiceImpl implements INexenWarrantycardService 
{
    @Value("${mini-api}")
    private String miniApiUrl;

    @Autowired
    private NexenBarcodeMapper nexenBarcodeMapper;
    @Autowired
    private NexenWarrantycardMapper nexenWarrantycardMapper;


    /**
     * 查询质保卡
     * 
     * @param Id 质保卡主键
     * @return 质保卡
     */
    @Override
    public NexenWarrantycard selectNexenWarrantycardById(Long Id)
    {
        NexenWarrantycard nexenWarrantycard = nexenWarrantycardMapper.selectNexenWarrantycardById(Id);
        String tireCode1 = nexenWarrantycard.getTireCode1();
        if (StringUtils.isNotBlank(tireCode1)) {
            nexenWarrantycard.setTireCode1Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode1));
        }
        String tireCode2 = nexenWarrantycard.getTireCode2();
        if (StringUtils.isNotBlank(tireCode2)) {
            nexenWarrantycard.setTireCode2Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode2));
        }
        String tireCode3 = nexenWarrantycard.getTireCode3();
        if (StringUtils.isNotBlank(tireCode3)) {
            nexenWarrantycard.setTireCode3Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode3));
        }
        String tireCode4 = nexenWarrantycard.getTireCode4();
        if (StringUtils.isNotBlank(tireCode4)) {
            nexenWarrantycard.setTireCode4Info(nexenBarcodeMapper.selectNexenBarcodeByCode(tireCode4));
        }
        return nexenWarrantycard;
    }

    /**
     * 查询质保卡列表
     * 
     * @param nexenWarrantycard 质保卡
     * @return 质保卡
     */
    @Override
    public List<NexenWarrantycard> selectNexenWarrantycardList(NexenWarrantycard nexenWarrantycard)
    {
        return nexenWarrantycardMapper.selectNexenWarrantycardList(nexenWarrantycard);
    }

    /**
     * 新增质保卡
     * 
     * @param nexenWarrantycard 质保卡
     * @return 结果
     */
    @Override
    public int insertNexenWarrantycard(NexenWarrantycard nexenWarrantycard)
    {
        return nexenWarrantycardMapper.insertNexenWarrantycard(nexenWarrantycard);
    }

    /**
     * 修改质保卡
     * 
     * @param nexenWarrantycard 质保卡
     * @return 结果
     */
    @Override
    public int updateNexenWarrantycard(NexenWarrantycard nexenWarrantycard)
    {
        return nexenWarrantycardMapper.updateNexenWarrantycard(nexenWarrantycard);
    }

    /**
     * 批量删除质保卡
     * 
     * @param Ids 需要删除的质保卡主键
     * @return 结果
     */
    @Override
    public int deleteNexenWarrantycardByIds(Long[] Ids)
    {
        return nexenWarrantycardMapper.deleteNexenWarrantycardByIds(Ids);
    }

    /**
     * 删除质保卡信息
     * 
     * @param Id 质保卡主键
     * @return 结果
     */
    @Override
    public int deleteNexenWarrantycardById(Long Id)
    {
        return nexenWarrantycardMapper.deleteNexenWarrantycardById(Id);
    }

    /**
     * 审核质保卡
     *
     * @param nexenWarrantycard
     * @return
     */
    @Override
    public int updateAuditStatus(NexenWarrantycard nexenWarrantycard) {
        int i = nexenWarrantycardMapper.updateAuditStatus(nexenWarrantycard);
        if (i == 1) {
            Long id = nexenWarrantycard.getId();
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("id", id);
            jsonBody.put("type","card" );
            // 发送POST请求，并设置请求的URL、请求头和请求体
            String result = HttpUtil.createPost(miniApiUrl)
                    .header(Header.CONTENT_TYPE, "application/json; charset=utf-8")
                    .body(jsonBody.toString())
                    .execute()
                    .body();
            // 异步记录日志
            SysOperLog sysOperLog = new SysOperLog();
            sysOperLog.setTitle("质保卡服务消息");
            sysOperLog.setBusinessType(2);
            sysOperLog.setOperatorType(1);
            sysOperLog.setOperUrl(miniApiUrl);
            sysOperLog.setOperParam(jsonBody.toString());
            sysOperLog.setRequestMethod("POST");
            sysOperLog.setOperName(nexenWarrantycard.getAuditBy());
            sysOperLog.setJsonResult(result);
            sysOperLog.setOperIp("内网IP");
            sysOperLog.setStatus(0);
            sysOperLog.setOperTime(DateUtil.date());
            AsyncManager.me().execute(AsyncFactory.recordOper(sysOperLog));
        }
        return i;
    }

    @Override
    public List<NexenWarrantycard> selectAllList(NexenWarrantycard nexenWarrantycard) {
        return nexenWarrantycardMapper.selectAllList(nexenWarrantycard);
    }

    @Override
    public int updateNexenServicerequestSendSMSByIds(List<Long> ids) {
        return nexenWarrantycardMapper.updateNexenServicerequestSendSMSByIds(ids);
    }

}
